package com.example.jz.noone.activity;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.jz.noone.R;
import com.example.jz.noone.fragment.FragmentHome;
import com.example.jz.noone.fragment.FragmentMe;
import com.example.jz.noone.utils.StatusBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportActivity;

public class FragmentationActivity extends SupportActivity implements RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.frame_layout)
    FrameLayout frameLayout;
    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_me)
    RadioButton rbMe;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    private int curPosition = 0; //当前显示的页码
    private FragmentHome fragmentHome;
    private FragmentMe fragmentMe;
    private long firstTime = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_fragment );
        ButterKnife.bind( this );
        radioGroup.setOnCheckedChangeListener(this);
        initFragment();
    }

    private void initFragment() {
        fragmentHome = findFragment( FragmentHome.class );
        if (fragmentHome!=null){
            fragmentMe = findFragment( FragmentMe.class );
        }else {
            fragmentHome = new FragmentHome();
            fragmentMe = new FragmentMe();
            loadMultipleRootFragment( R.id.frame_layout,curPosition,
                    fragmentHome,
                    fragmentMe);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group,int checkedId) {
        switch (checkedId){
            case R.id.rb_home:
                showHideFragment( fragmentHome,getPreFragment() );
                curPosition = 0;
                break;
            case R.id.rb_me:
                showHideFragment( fragmentMe,getPreFragment() );
                curPosition = 1;
                break;
        }
    }
    private ISupportFragment getPreFragment(){
        switch (curPosition){
            case 0:
                return fragmentHome;
            case 1:
                return fragmentMe;
        }
        return null;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                System.exit(0);
            } else {
                Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

}
